use std::{env, process::Command};

const WIDTH: usize = 16;
// const ON_ENVS = ["HOME", ""];

pub fn print() {
    // 打印软件版本
    // 获取编译的时间
    let head_version = match option_env!("HEAD_VERSION") {
        Some(k) => {
            let (first, _last) = k.split_at(7);
            format!(", build {}", first)
        },
        None => String::new(),
    };

    let build_time = match option_env!("BUILD_TIME") {
        Some(k) => format!(" on {}", k),
        None => String::new(),
    };

    log::info!(
        "{} {}.{}.{}{}{}{} {}/{}",
        env!("CARGO_PKG_NAME"),
        env!("CARGO_PKG_VERSION_MAJOR"),
        env!("CARGO_PKG_VERSION_MINOR"),
        env!("CARGO_PKG_VERSION_PATCH"),
        option_env!("CARGO_PKG_VERSION_PRE").unwrap_or(""),
        head_version,
        build_time,
        env::consts::OS,
        env::consts::ARCH,
    );
    log::info!(
        "{} version {}{}",
        env!("CARGO_PKG_DESCRIPTION"),
        env!("CARGO_PKG_VERSION"),
        build_time
    );
    log::info!("copyright (c) 2024, liaobaikai.  All rights reserved.");

    log::info!("{:<WIDTH$} {:?}", "AYISHA_HOME:", env::var("AYISHA_HOME").unwrap());
    // log::info!("{:<WIDTH$} {}", "Config file:", &args.config_file);
    // log::info!("{:<WIDTH$} {}", "Log config file:", &args.log_conf_file);
    print_os_info();

    
    // 打印环境变量
    for (k, v) in env::vars() {
        // if k.starts_with("AYISHA_") 
        // || k.starts_with("LANG") 
        // || k.starts_with("PATH") 
        // || k.starts_with("PWD") 
        // || k.starts_with("OLDPWD") 
        // || k.starts_with("USER") {
        //     log::info!("+++ {}={}", k, v);
        // }
        // env::remove_var(k);
        log::info!("+++ {}={}", k, v);
    }

    // 打印系统信息
}



fn print_os_info() {
    log::info!("{:<WIDTH$} {}", "System name:", env::consts::OS);
    log::info!("{:<WIDTH$} {}", "Machine:", env::consts::ARCH);

    if cfg!(target_os = "windows") {

        match Command::new("cmd").arg("/c").arg("hostname").output() {
            Ok(output) => {
                log::info!(
                    "{:<WIDTH$} {}",
                    "Host name:",
                    String::from_utf8_lossy(&output.stdout).trim_end_matches("\n")
                );
            }
            Err(e) => {
                log::error!("Execute error, cause: {}", e);
            }
        }

        match local_ip_address::local_ip() {
            Ok(my_local_ip) => {
                log::info!("{:<WIDTH$} {:?}", "Local IP Addr:", my_local_ip);
            },
            Err(e) => {
                log::error!("{:<WIDTH$} {}, cause: {e}", "Local IP Addr:", "NULL");
            }
        }

    } else if cfg!(all(unix)) {
        // log::info!("{:<WIDTH$} {}", "System name:", env::consts::OS);

        // match Command::new("sh").arg("-c").arg("uname").output() {
        //     Ok(output) => {
        //         log::info!(
        //             "{:<WIDTH$} {}",
        //             "System name:",
        //             String::from_utf8_lossy(&output.stdout).trim_end_matches("\n")
        //         );
        //     }
        //     Err(e) => {
        //         log::error!("Execute error, cause: {}", e);
        //     }
        // }

        match Command::new("sh").arg("-c").arg("hostname").output() {
            Ok(output) => {
                log::info!(
                    "{:<WIDTH$} {}",
                    "Host name:",
                    String::from_utf8_lossy(&output.stdout).trim_end_matches("\n")
                );
            }
            Err(e) => {
                log::error!("Execute error, cause: {}", e);
            }
        }

        match local_ip_address::local_ip() {
            Ok(my_local_ip) => {
                log::info!("{:<WIDTH$} {:?}", "Local IP Addr:", my_local_ip);
            },
            Err(e) => {
                log::error!("{:<WIDTH$} {}, cause: {e}", "Local IP Addr:", "NULL");
            }
        }

        match Command::new("sh").arg("-c").arg("uname -r").output() {
            Ok(output) => {
                log::info!(
                    "{:<WIDTH$} {}",
                    "Release:",
                    String::from_utf8_lossy(&output.stdout).trim_end_matches("\n")
                );
            }
            Err(e) => {
                log::error!("Execute error, cause: {}", e);
            }
        }

        match Command::new("sh").arg("-c").arg("uname -v").output() {
            Ok(output) => {
                log::info!(
                    "{:<WIDTH$} {}",
                    "Version:",
                    String::from_utf8_lossy(&output.stdout).trim_end_matches("\n")
                );
            }
            Err(e) => {
                log::error!("Execute error, cause: {}", e);
            }
        }

        // uname -v
        // match Command::new("sh").arg("-c").arg("cat /etc/os-release | grep PRETTY_NAME").output() {
        //     Ok(output) => {
        //         log::info!(
        //             "{:<WIDTH$} {}",
        //             ":",
        //             String::from_utf8_lossy(&output.stdout).trim_end_matches("\n")
        //         );
        //     }
        //     Err(e) => {
        //         log::error!("Execute error, cause: {}", e);
        //     }
        // }
    }

    // if cfg!(target_os = "macos") {
        // match Command::new("sh")
        //     .arg("-c")
        //     .arg("sw_vers --productVersion")
        //     .output()
        // {
        //     Ok(output) => {
        //         log::info!(
        //             "{:<WIDTH$} {}",
        //             "Version2:",
        //             String::from_utf8_lossy(&output.stdout).trim_end_matches("\n")
        //         );
        //     }
        //     Err(e) => {
        //         log::error!("Execute error, cause: {}", e);
        //     }
        // }
    // }

    // uname -n
}
